<?php

namespace App\Kuafu\Config\Controller\Pos;

use App\Kuafu\Config\Enum\ConfigCodeEnum;
use App\Kuafu\Config\Enum\ConfigEnum;
use App\Kuafu\Config\Service\ConfigService;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use phpDocumentor\Reflection\Types\Collection;

class ConfigController extends BaseController
{

    /**
     * 获取通用配置接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request)
    {
        $type = $request->get('type');

        if (empty($type)) {
            return $this->fail(ConfigCodeEnum::ERR_CONFIG_PARAM);
        }

        $lists = ConfigService::listByTypeAndKey($type);

        foreach ($lists as &$one) {
            if (isJson($one)) {
                $one = json_decode($one, 1);
            }
        }

        $data = [
            'lists' => $lists,
        ];

        return $this->success($data);
    }

    /**
     * 根据type查询列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOrderRemark(Request $request)
    {
        $type = 'orderRemark';

        $lists = ConfigService::listByTypeAndKey($type);

        $lists = collect($lists)->sortKeys()->values();

        $data = [
            'lists' => $lists,
        ];

        return $this->success($data);
    }
}
